var xanhte_admin_qcart = Class.create();
xanhte_admin_qcart.prototype = {
    
  fileSelector: 0,
  msg: null,
  
  initialize: function(msg){
      // error msg
      this.msg = msg;
      // assign onclick ..
      if ($('i_noexpire')){
          if ($('i_noexpire').checked){
              Element.hide('i_expire_input');
          }
          Event.observe($('i_noexpire'), 'click', function(){
              if (this.checked){
                  Element.hide('i_expire_input');
              } else {
                  Element.show('i_expire_input');
              }
          });
      }
      if ($('i_norelease')){
          if ($('i_norelease').checked){
              Element.hide('i_release_input');
          }
          Event.observe($('i_norelease'), 'click', function(){
              if (this.checked){
                  Element.hide('i_release_input');
              } else {
                  Element.show('i_release_input');
              }
          });
      }
  },
    
  appendFileSelector: function(selDom){
      this.fileSelector++;

      var div = $(selDom);
      if (!div) return ;
      
      var input = $(selDom).getElementsByTagName('INPUT');
      if (input){
          for (var i=0,j=input.length; i<j; i++){
              if (input[i].type!='undefined' && input[i].type=="file"){
                  var n_input = input[i].cloneNode(true);
                  n_input.value = '';
                  div.insertBefore(n_input, div.lastChild);
                  div.insertBefore(document.createElement('br'), div.lastChild);
                  break;
              }
          }
      }
      return ;
  },
  
  appendEventsToItemList: function(){
      /// delele all checkbox
      if ($('deleteSumChk')){
          Event.observe($('deleteSumChk'), 'click', (function(self){
              return function(){
                  var chks = document.getElementsByTagName('INPUT');
                  for (var i=0,j=chks.length; i<j; i++){
                      if (chks[i].type=="checkbox" && chks[i].id != this.id){
                          chks[i].checked = this.checked ;
                      }
                  }
              };
          })(this));
      }
      // select action
      var d = document.getElementsByTagName('SELECT');
      for (var i=0,j=d.length; i<j; i++){
          if (d[i].id.match(/^i_status_(\d+)$/)){
              var id = RegExp.$1;
              var className = d[i].value==0 ? "invalid" : "valid";
              /* Element.addClassName(d[i].parentNode, className); */
              d[i].parentNode.className = className;
              Event.observe(d[i], 'change', (function(id,self){
                  return function(){
                      self.changeStatus(id, this);
                  };
              })(id,this));
              
          } else if (d[i].id.match(/^i_stock_(\d+)$/)){
              var id = RegExp.$1
              var className = d[i].value==0 ? "red" : (d[i].value==1 ? "blue" : "yellow");
              d[i].parentNode.className = className;
              Event.observe(d[i], 'change', (function(id,self){
                  return function(){
                      self.changeStock(id, this);
                  };
              })(id,this));
          }
      }
      // delete all button
      if ($('dodeleteAll')){
          Event.observe($('dodeleteAll'), 'click', function(self){
              return function(){
                  if (confirm(self.msg.checkDeleteAll)){
                      $('itemlist').submit();
                  }
              }
          }(this));
      }
  },
  
  changeStatus: function(id,sel){
      sel.disabled = true;
      var ajax = new Ajax.Request('../json.php',{
        method : 'post',
        parameters: 'action_admin_json_qcart_item_changestatus=1&i_id='+id+'&i_status='+sel.value+'&'+Math.random()*100,
        requestHeaders: ['Referer', xoops_url],
        onSuccess: function(httpobj,json){
            if (json.result && json.result==1){
                sel.parentNode.className = sel.value==0 ? "invalid" : "valid";
            }
        },
        onComplete: function(h,j){
            sel.disabled = false;
        }
      });
  },
  changeStock: function(id,sel){
      sel.disabled = true;
      var ajax = new Ajax.Request('../json.php',{
        method : 'post',
        parameters: 'action_admin_json_qcart_item_changestock=1&i_id='+id+'&i_stock='+sel.value+'&'+Math.random()*100,
        requestHeaders: ['Referer', xoops_url],
        onSuccess: function(httpobj,json){
            if (json.result && json.result==1){
                sel.parentNode.className = sel.value==0 ? "red" : (sel.value==1 ? "blue" : "yellow");
            }
        },
        onComplete: function(h,j){
            sel.disabled = false;
        }
      });
  }
};

